package cc.mrbird.febs.accounting.controller;

import cc.mrbird.febs.accounting.entity.ProjectType;
import cc.mrbird.febs.accounting.service.IProjectTypeService;
import cc.mrbird.febs.common.annotation.ControllerEndpoint;
import cc.mrbird.febs.common.controller.BaseController;
import cc.mrbird.febs.common.entity.FebsConstant;
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.common.utils.FebsUtil;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 *  Controller
 *
 * @author chenyuxuan
 * @date 2021-09-28 23:26:45
 */
@Slf4j
@Validated
@Controller
public class ProjectTypeController extends BaseController {

    @Resource
    private IProjectTypeService projectTypeService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "projectType")
    public String projectTypeIndex(){
        return FebsUtil.view("project/projectType");
    }

    @GetMapping("projectType")
    @ResponseBody
    public FebsResponse getAllProjectTypes(ProjectType projectType) {
        return new FebsResponse().success().data(projectTypeService.findProjectTypes(projectType));
    }

    @GetMapping("projectType/list")
    @ResponseBody
    public FebsResponse projectTypeList(QueryRequest request, ProjectType projectType) {
        Map<String, Object> dataTable = getDataTable(this.projectTypeService.queryProjectTypes(request, projectType));
        return new FebsResponse().success().data(dataTable);
    }


    @ControllerEndpoint(operation = "新增ProjectType", exceptionMessage = "新增ProjectType失败")
    @PostMapping("projectType")
    @ResponseBody
    @RequiresPermissions("projectType:add")
    public FebsResponse addProjectType(@Valid ProjectType projectType) {
        boolean success =  this.projectTypeService.createProjectType(projectType);
        if (success) {
            return new FebsResponse().success();
        } else {
            return new FebsResponse().fail().message("业务类型已存在，请修改!");
        }
    }

    @ControllerEndpoint(operation = "删除ProjectType", exceptionMessage = "删除ProjectType失败")
    @GetMapping("projectType/delete/{projectTypeIds}")
    @ResponseBody
    @RequiresPermissions("projectType:delete")
    public FebsResponse deleteProjectType(@NotBlank(message = "{required}") @PathVariable String projectTypeIds) {
        boolean success = this.projectTypeService.deleteProjectType(projectTypeIds);
        if (success) {
            return new FebsResponse().success();
        } else {
            return new FebsResponse().fail().message("业务类型已被使用，无法删除!");
        }
    }

    @ControllerEndpoint(operation = "修改ProjectType", exceptionMessage = "修改ProjectType失败")
    @PostMapping("projectType/update")
    @ResponseBody
    @RequiresPermissions("projectType:update")
    public FebsResponse updateProjectType(ProjectType projectType) {
        boolean success = this.projectTypeService.updateProjectType(projectType);
        if (success) {
            return new FebsResponse().success();
        } else {
            return new FebsResponse().fail().message("业务类型已存在，请修改!");
        }
    }

    @ControllerEndpoint(operation = "导出ProjectType", exceptionMessage = "导出Excel失败")
    @PostMapping("projectType/excel")
    @ResponseBody
    @RequiresPermissions("projectType:excel")
    public void export(QueryRequest queryRequest, ProjectType projectType, HttpServletResponse response) {
        List<ProjectType> projectTypes = this.projectTypeService.queryProjectTypes(queryRequest, projectType).getRecords();
        ExcelKit.$Export(ProjectType.class, response).downXlsx(projectTypes, false);
    }
}
